import Vue from 'vue'
import Router from 'vue-router'
import Login from './components/Login.vue'

//组件懒加载
const Home = () =>
    import ('./components/Home.vue')
    // 欢迎页
const Welcome = () =>
    import ('./components/Welcome.vue')
const Users = () =>
    import ('./components/user/Users.vue')
    // 权限管理组件
const Rights = () =>
    import ('./components/power/Rights.vue')
    // 角色管理组件
const Roles = () =>
    import ('./components/power/Roles.vue')
const Cate = () =>
    import ('./components/goods/Cate.vue')
const Params = () =>
    import ('./components/goods/Params.vue')
const List = () =>
    import ('./components/goods/List.vue')

const GoodAdd = () =>
    import ('./components/goods/GoodAdd.vue')

const Order = () =>
    import ('./components/order/Order.vue')
const Report = () =>
    import ('./components/report/Report.vue')



// 安装路由
Vue.use(Router)

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: [{
            path: '/',
            redirect: '/login'
        },
        {
            path: '/login',
            name: 'login',
            component: Login
        }, {
            path: '/home',
            name: 'home',
            redirect: '/welcome',
            component: Home,
            children: [{
                    path: '/welcome',
                    component: Welcome
                }, {
                    path: '/users',
                    component: Users
                }, {
                    path: '/rights',
                    component: Rights
                }, {
                    path: '/roles',
                    component: Roles
                }, {
                    path: '/categories',
                    component: Cate
                },
                {
                    path: '/params',
                    component: Params
                }, {
                    path: '/goods',
                    component: List
                },
                {
                    path: '/goods/add',
                    component: GoodAdd
                },
                {
                    path: '/orders',
                    component: Order
                },
                {
                    path: '/reports',
                    component: Report
                }
            ]
        }
    ]
})


router.beforeEach((to, from, next) => {
    //to将要访问路径
    // from从哪跳转而来
    // next放行函数 

    if (to.path == '/login') return next();
    // 获取token
    const tokenStr = window.sessionStorage.getItem('token');
    // 判断本地token是否存在，不存在强制跳转到登录页
    if (!tokenStr) return next('/login');
    // 存在则放行
    return next();


})
export default router